//
//  ByUISafetyCerUploadVC.m
//  chequanbao_ios
//
//  Created by pro on 2017/6/20.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUISafetyCerUploadVC.h"

// api
#import "ByApiSafety.h"
#import "ByApiUser.h"

@interface ByUISafetyCerUploadVC ()
// other
@property (nonatomic, strong) ByIconChooseDataSource *iconChooseDataSource;

// view
@property (nonatomic, strong) UIImageView *tapView;

// data
@property (nonatomic, strong) NSString *imgStr;
@end

@implementation ByUISafetyCerUploadVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"提交凭证";
    [self addLeftBackBarButtonItem];
    
    [self createViews];
}

#pragma mark -- getter
- (ByIconChooseDataSource *)iconChooseDataSource {
    if (!_iconChooseDataSource) {
        _iconChooseDataSource = [[ByIconChooseDataSource alloc] init];
        [_iconChooseDataSource fireWithViewController:self];
    }
    return _iconChooseDataSource;
}


#pragma mark -- 创建视图
- (void)createViews {
    
//    UIControl *tapView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.4, SCREEN_WIDTH * 0.4)];
//    tapView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
//    tapView.x = SCREEN_WIDTH / 2;
//    tapView.bottom = SCREEN_HEIGHT / 2;
//    [tapView addTarget:self action:@selector(uploadClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:tapView];
    
    _tapView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.4, SCREEN_WIDTH * 0.4)];
    _tapView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    _tapView.x = SCREEN_WIDTH / 2;
    _tapView.bottom = SCREEN_HEIGHT / 2;
//    [tapView addTarget:self action:@selector(uploadClick) forControlEvents:UIControlEventTouchUpInside];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(uploadClick)];
    _tapView.userInteractionEnabled = YES;
    [_tapView addGestureRecognizer:tap];
    [self.view addSubview:_tapView];
    
    UIImageView *tapImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _tapView.width * 0.3, _tapView.width * 0.3)];
    tapImageView.image = [UIImage imageNamed:@"Safety_add_with_border"];
    [_tapView addSubview:tapImageView];
    tapImageView.backgroundColor = [UIColor clearColor];
    tapImageView.x = _tapView.width / 2;
    tapImageView.bottom = _tapView.height / 2;
    
    
   
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _tapView.width, 20)];
    noteLabel.top = tapImageView.bottom + 10;
    noteLabel.adjustsFontSizeToFitWidth = YES;
    noteLabel.font = FONT(15);
    noteLabel.text = @"点击上传";
    noteLabel.textColor = [UIColor whiteColor];
    noteLabel.textAlignment = NSTextAlignmentCenter;
    noteLabel.backgroundColor = [UIColor clearColor];
    [_tapView addSubview:noteLabel];
    
    
    
    UILabel *noteLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _tapView.width, 20)];
    noteLabel2.x = _tapView.x;
    noteLabel2.top = _tapView.bottom + 20;
    noteLabel2.adjustsFontSizeToFitWidth = YES;
    noteLabel2.font = FONT(18);
    noteLabel2.text = @"上传微信支付凭证";
    noteLabel2.textColor = [UIColor whiteColor];
    noteLabel2.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:noteLabel2];
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = rgba(23, 160, 255, 1);
    [button setFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(commit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    button.bottom = SCREEN_HEIGHT;
}


#pragma mark -- 相关事件
- (void)uploadClick{
    [self.iconChooseDataSource makeConfigToSelectIcon];
    __weak typeof(self) weakself = self;
    [self.iconChooseDataSource setCompleteBlock:^(UIImage *image) {
        weakself.tapView.image = image;
    }];

}

- (void)commit {
    if (!self.tapView.image) {
        return;
    }
    [self performSelector:@selector(afterLoad) withObject:nil afterDelay:0.01];
    [ByApiUser uploadCer:self.tapView.image success:^(ByNetBaseResult *result) {
        if ([result.data isKindOfClass:[NSDictionary class]]) {
            ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
            model.img = [result.data objectForKey:@"id"];
            model.insurance_id = self.insurance_id;
            [ByApiSafety uploadSafetyCer:model success:^(ByNetBaseResult *result) {
                [ByCoreToolAlert showOnlyText:result.data];
                [self performSelector:@selector(popVC) withObject:nil afterDelay:1];
            } failure:^(ByNetBaseResult *result) {
                [ByCoreToolAlert showOnlyText:result.msg];
            }];
        }
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
    
}

- (void)afterLoad{
    [ByCoreToolAlert showLoadind:@"上传中..."];
    
}

- (void)popVC{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
